﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SchoolPrintDomin;
using System.Net;

namespace SchoolPrint
{
    public partial class frmMain : Form
    {
        private double sum;
        //private string dayAddress = Application.StartupPath + "\\文件数据\\" + DateTime.Now.AddDays(1).ToLongDateString();

        //static DALService.DALWebService service = new DALService.DALWebService();
        static WebReference.DALWebService service = new WebReference.DALWebService();
        static DominInfo dominInfo = new DominInfo();
        private string dayAddress;
        private string check;
        public static frmMain fatherForm;
        public double Sum
        {
            get
            {
                return sum;
            }
            set
            {
                sum = value;
            }
        }
        public frmMain()
        {
            InitializeComponent();
            button1.Text = "登录系统";
            label1.Text = "打印店：未登录";
            label5.Text = sum.ToString() + "元";
            //MessageBox.Show(times);
            service.Credentials = new NetworkCredential(dominInfo.UserName, dominInfo.Password, dominInfo.Domin);
            fatherForm = this;
            try
            {
                StreamReader swr = File.OpenText(Application.StartupPath + "\\文件数据\\test");
                string name = swr.ReadLine();
                string password = swr.ReadLine();
                swr.Close();
                textBox1.Text = name;
                textBox2.Text = password;
            }
            catch 
            {
                ;
            }
        }

        private ListboxItem[] item = new ListboxItem[500];

        private void frmMain_Load(object sender, EventArgs e)
        {
            //初始化文档以及总金额
            if (!Directory.Exists(Application.StartupPath + "\\文件数据\\" + DateTime.Now.AddDays(1).ToLongDateString()))
            {
                if (!Directory.Exists(Application.StartupPath + "\\文件数据\\" + DateTime.Now.ToLongDateString().ToString()))
                {
                    Directory.CreateDirectory(Application.StartupPath + "\\文件数据\\" + DateTime.Now.ToLongDateString().ToString());
                }
                dayAddress = Application.StartupPath + "\\文件数据\\" + DateTime.Now.ToLongDateString().ToString();
                check = DateTime.Now.ToLongDateString().ToString();
                ListboxItem.dayAddress = dayAddress;
                ListboxItem.check = check;
            }
            else
            {
                dayAddress = Application.StartupPath + "\\文件数据\\" + DateTime.Now.AddDays(1).ToLongDateString();
                check = DateTime.Now.AddDays(1).ToLongDateString();
                ListboxItem.dayAddress = dayAddress;
                ListboxItem.check = check;
            }
            try
            {
                /*从加密文件恢复原文件*/
                StreamReader srs = File.OpenText(dayAddress + "\\" + "今日打印.la");
                string temps = srs.ReadToEnd();
                srs.Close();
                byte[] changs = Encoding.Unicode.GetBytes(temps);

                for (int i = 0; i < changs.Length; i++)
                {
                    changs[i] -= 21;
                }
                temps = Encoding.Unicode.GetString(changs);
                StreamWriter srsw = new StreamWriter(dayAddress + "\\" + "今日打印情况.txt", false);
                srsw.Write(temps);
                srsw.Close();
                /****************************************/
                StreamReader sr = File.OpenText(dayAddress + "\\" + "今日打印情况.txt");
                label5.Text = sr.ReadLine();
                sr.Close();
                label5.Text = label5.Text.Substring(label5.Text.IndexOf("：")+1);
                sum = Convert.ToInt32(label5.Text.Substring(0, label5.Text.IndexOf("元")));
            }
            catch 
            {
                label5.Text = "0元";
            }
            MessageBox.Show("请记得在左边登陆后查看今日打印信息！");
            //MessageBox.Show(DateTime.Now.ToLocalTime().ToString());

            ////方法使用实例
            ////bool result1 = service.Login("1026347293@qq.com", "123456");
            //DataSet result2 = service.GetFileList("1");
            ////bool result3 = service.EnsureDownload("documentid");
            ////通过以下方法可访问DataSet里面的数据
            //{
            //    DataTable table = result2.Tables[0];
            //    int count = table.Rows.Count;
            //    string[] CustomerId = new string[count];
            //    string[] DocumentURL = new string[count];
            //    string[] DocumentId = new string[count];
            //    string[] DocumentName = new string[count];
            //    string[] DocumentType = new string[count];
            //    string[] DocumentWay = new string[count];
            //    string[] DocumentState = new string[count];
            //    string[] DocumentAddress = new string[count];
            //    string[] ShoperId = new string[count];
            //    string[] DocumentNum = new string[count];
            //    string[] SubmitTime = new string[count];
            //    int i = 0;
            //    foreach (DataRow row in table.Rows)
            //    {
            //        CustomerId[i] = row["CustomerId"].ToString();
            //        DocumentURL[i] = row["DocumentURL"].ToString();
            //        DocumentId[i] = row["DocumentId"].ToString();
            //        DocumentName[i] = row["DocumentName"].ToString();
            //        DocumentType[i] = row["DocumentType"].ToString();
            //        DocumentWay[i] = row["DocumentWay"].ToString();
            //        DocumentState[i] = row["DocumentState"].ToString();
            //        DocumentAddress[i] = row["DocumentAddress"].ToString();
            //        ShoperId[i] = row["ShoperId"].ToString();
            //        DocumentNum[i] = row["DocumentNum"].ToString();
            //        SubmitTime[i] = row["SubmitTime"].ToString();
            //        i++;
            //    }
            //}

        }

        private void refrash()
        {
            plDownList.Controls.Clear(); //刷新时先清空容器

            //从服务器获取打印列表
            //DataSet dataSet = new DataSet();
            //int table, rows;
            //string[] paperType = null, copies = null, printMode = null, remark = null, documentUrl = null, documentName = null, address = null;
            //int[] ID=null;
            //dataSet = service.GetFileList(textBox1.Text);
            //table = dataSet.Tables.Count;
            //rows = dataSet.Tables["Columns"].Rows.Count;
            //for (int i = 0; i < rows; i++)
            //{
            //    paperType[i] = (string)dataSet.Tables["Columns"].Rows[i]["PaperType"];
            //    copies[i] = (string)dataSet.Tables["Columns"].Rows[i]["Copies"];
            //    printMode[i] = (string)dataSet.Tables["Columns"].Rows[i]["PrintType"];
            //    remark[i] = (string)dataSet.Tables["Columns"].Rows[i]["Remark"];
            //    documentUrl[i] = (string)dataSet.Tables["Columns"].Rows[i]["DocumentUrl"];
            //    documentName[i] = (string)dataSet.Tables["Columns"].Rows[i]["DocumentName"];
            //    address[i] = (string)dataSet.Tables["Columns"].Rows[i]["address"];
            //    ID[i] = (int)dataSet.Tables["Columns"].Rows[i]["ID"];
            //}
            ////int i = 0;
            ////foreach (DataRow dr1 in dataSet.Tables["tab2 "].Rows)
            ////{
            ////    paperType[i] = dr1["PaperType"].ToString();
            ////    copies[i] = dr1["Copies"].ToString();
            ////    printMode[i] = dr1["PrintMode"].ToString();


            ////}

            MessageBox.Show(textBox1.Text);
            WebReference.Document[] result2 = service.GetDocList(textBox1.Text);
            //int count = table.Rows.Count;
            int count = result2.Length;
            string[] CustomerId = new string[count];
            string[] DocumentUrl = new string[count];
            long[] ID = new long[count];
            string[] DocumentName = new string[count];
            string[] PaperType = new string[count];
            string[] sendWay = new string[count];
            string[] State = new string[count];
            string[] address = new string[count];
            string[] ShoperId = new string[count];
            string[] Copies = new string[count];
            string[] PrintMode = new string[count];
            string[] Remark = new string[count];
            string[] Phone = new string[count];
            string[] Names = new string[count];
            //string[] DocumentNum = new string[count];
            //string[] SubmitTime = new string[count];
            int k = 0;
            foreach (WebReference.Document row in result2)
            {
                //CustomerId[k] = row["CustomerId"].ToString();
                CustomerId[k] = row.CustomerId;
                DocumentUrl[k] = row.DocumentUrl + "httt://xiaoyintong.com//Upload//" + row.DocumentName;
                ID[k] = row.ID;
                DocumentName[k] = row.DocumentName;
                PaperType[k] = row.PaperType;
                sendWay[k] = row.sendWay;
                State[k] = row.State;
                address[k] = row.address;
                ShoperId[k] = row.ShoperId;
                Copies[k] = row.Copies;
                PrintMode[k] = row.PrintMode;
                Remark[k] = row.Remark;
                Phone[k] = row.Phone;
                Names[k] = row.Name;
                //DocumentNum[i] = row["DocumentNum"].ToString();
                //SubmitTime[i] = row["SubmitTime"].ToString();
                k++;
            }


            for (int i = 0; /*i < rows*/(i < count)&&(State[i]=="打印中")/*i<7*/; i++)
            {
                //item[i] = new ListboxItem("http://softdownload.hao123.com/hao123-soft-online-bcs/soft/Q/2013-10-17_QQ2013SP3.exe", "G:", "file" + i + ".ppt", new Random().Next(10000, 20000) + "", this,dayAddress);
                //item[i] = new ListboxItem("http://xiaoyintong.com//Upload//test.doc", "test" + i + ".doc", new Random().Next(10000, 20000) + "", this, "A4", "4份", "单面", "好一点", 23, "陈楷佳", "12345678");
                item[i] = new ListboxItem(DocumentUrl[i], DocumentName[i], address[i], this, PaperType[i], Copies[i], PrintMode[i], Remark[i], (int)ID[i], Names[i], Phone[i]);
                item[i].Name = i.ToString();
                item[i].Dock = System.Windows.Forms.DockStyle.Top;
                item[i].Location = new System.Drawing.Point(0, 80 * i);
                item[i].Name = "item" + i;
                item[i].Size = new System.Drawing.Size(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width, 80);
                item[i].MinimumSize = new Size(450, 80);
                item[i].TabIndex = i;
                this.plDownList.Controls.Add(item[i]);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (button1.Text == "登录系统")
                {
                    if (/*true*/service.Login(textBox1.Text, textBox2.Text))//登陆成功
                    {
                        label1.Text = "打印店：" + textBox1.Text;
                        //button1.Text = "注销登录";
                        button1.Text = "刷新";
                        try
                        {
                            StreamReader swr = File.OpenText(Application.StartupPath + "\\文件数据\\test");
                            string name=swr.ReadLine();
                            swr.Close();
                            if(string.Compare(name,textBox1.Text)!=0)
                            {
                                if (MessageBox.Show("记住密码？", "确认信息", MessageBoxButtons.OKCancel) == DialogResult.OK)
                                {
                                    StreamWriter swrw = new StreamWriter(Application.StartupPath + "\\文件数据\\test", false);
                                    swrw.WriteLine(textBox1.Text);
                                    swrw.WriteLine(textBox2.Text);
                                    swrw.Close();
                                }
                            }
                        }
                        catch
                        {
                            if (MessageBox.Show("记住密码？", "确认信息", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                StreamWriter sw = File.CreateText(Application.StartupPath + "\\文件数据\\test");
                                sw.WriteLine(textBox1.Text);
                                sw.WriteLine(textBox2.Text);
                                sw.Close();
                            }
                        }
                        
                        refrash();
                    }
                    else
                    {
                        MessageBox.Show("该打印店没有注册！");
                    }
                }
                else if(button1.Text=="刷新")
                {
                    refrash();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button1.Text != "登录系统")
            {
                if (MessageBox.Show("确定将今天金额清零？", "确认信息", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    SureReset SureForm = new SureReset();
                    SureForm.Show();
                    //新建第二天的文件夹,文件夹名为第二天时间+星期
                    //dayAddress = Application.StartupPath + "\\文件数据\\" + DateTime.Now.AddDays(1).ToLongDateString();
                    //check = DateTime.Now.AddDays(1).ToLongDateString();
                    //ListboxItem.dayAddress = dayAddress;
                    //ListboxItem.check = check;
                    //Directory.CreateDirectory(dayAddress);
                    ////MessageBox.Show(address);
                    ////delete 
                    //sum = 0.0;
                    //label5.Text = sum.ToString() + "元";
                }
            }
            else
            {
                MessageBox.Show("您还未登录！");
            }
        }

        public void NewFileFolder()
        {
            dayAddress = Application.StartupPath + "\\文件数据\\" + DateTime.Now.AddDays(1).ToLongDateString();
            check = DateTime.Now.AddDays(1).ToLongDateString();
            ListboxItem.dayAddress = dayAddress;
            ListboxItem.check = check;
            Directory.CreateDirectory(dayAddress);
            //MessageBox.Show(address);
            //delete 
            sum = 0.0;
            label5.Text = sum.ToString() + "元";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (button1.Text != "登录系统")
            {
                if (File.Exists(dayAddress + "\\" + "今日打印情况.txt"))
                {
                    System.Diagnostics.Process.Start(dayAddress + "\\" + "今日打印情况.txt");
                }
                else
                {
                    MessageBox.Show("今日没有打印数据");
                }
            }
            else
            {
                MessageBox.Show("您还未登录！");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int err = 0;
            //MessageBox.Show("caca");
            for (int i = 0; i <= item.Length/*Convert.ToInt32((sender as ListboxItem).Name)*/; i++)
            {
                try
                {
                    item[i].ListboxItem_Start(sender, e);
                }
                catch (System.Exception ex)
                {
                    err++;
                }
            }
            //MessageBox.Show(err.ToString());
        }
    }
}
